package org.hin.programmering1;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;

import org.hin.programmering1.engine.AnimatedSprite;
import org.hin.programmering1.engine.GameEngine;
import org.hin.programmering1.engine.GameObject;
import org.hin.programmering1.engine.Renderable;
import org.hin.programmering1.engine.Sprite;

/**
 * Just a test of the GameEngine
 * 
 * @author Meanz, Fredrik
 */
public class Game extends GameEngine {
	
	private static Game game;
	private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	private TestWorld testWorld;
	
	public Game() {
		/** Setup window properties **/
		game = this;
		start();
	}
	
	/**
	 * Adds an object to the engine
	 * @param go
	 * @return
	 */
	public static boolean addObject(GameObject go) {
		if(go == null) {
			return false;
		}
		if(go instanceof Renderable) {
			game.addRenderable((Renderable)go);
		}
		game.addGameObject(go);
		return true;
	}
	
	public static int getFPS() {
		return game.getGameFPS();
	}

	@Override
	public void gamePreload() {
		// TODO Auto-generated method stub
		testWorld = new TestWorld();
		testWorld.gameLoad();
		// 32x25, 32x20
		setSize(800, 640);
		setResizable(false);
		setLocation((int)(screenSize.getWidth()-getSize().width)/2, (int)(screenSize.getHeight()-getSize().height)/2);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}

	@Override
	public void gameUpdate() {
		// TODO Auto-generated method stub
		testWorld.gameUpdate();
	}
	
	@Override
	public void gameRender(Graphics g) {
		// TODO Auto-generated method stub
		testWorld.gameRender(g);
	}
	
	/**
	 * Adds an animation to the game engine
	 * @param x
	 * The X value on the screen to place the Animation
	 * @param y
	 * The Y value on the screen to place the Animation
	 * @param speed
	 * The speed which the animation is going to move after
	 * @param imageNames
	 * The image locations for the frames in the animation
	 * @return
	 * The Animation that was added
	 */
	public static AnimatedSprite addAnimation(int x, int y, float speed, String... imageNames) {
		AnimatedSprite animation = AnimatedSprite.loadAnimation(speed, imageNames);
		Game.addObject(animation);
		animation.transform.setPosition(x, y);
		return animation;
	}
	
	/**
	 * Adds a Sprite to the game engine
	 * @param fileName
	 * The filename to the Sprite image
	 * @param x
	 * The X value on the screen to place the Sprite
	 * @param y
	 * The Y value on the screen to place the Sprite
	 * @return
	 * The Sprite that was added
	 */
	public static Sprite addSprite(String fileName, int x, int y) {
		Sprite spr = Sprite.loadSprite(fileName);
		spr.transform.position = new Point(x, y);
		Game.addObject(spr);
		return spr;
	}
	
	
}
